package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.RedPacketsActivityAdapter;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.CircleImageView;
import com.diudiutechnology.www.yadiudiu.util.view.UniversalPhotoPop;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RedPacketsModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.UniversalModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/30.
 * 红包
 */

public class RedPacketsActivity extends Activity implements View.OnClickListener,Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.listview)
    ListView listview;
    List<RedPacketsModel> getviewredpackets;
    RedPacketsActivityAdapter adapter;


    CircleImageView personal_icon;//头像

    TextView envelopes_money;//余额
    TextView red_envelopes;//抢到红包
    TextView balance;//活动红包总额
    TextView year;//年
    LinearLayout year_layout;//选择年份
    Context context;
    private MyProgressDialog mDialog;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    String str = "2017";//查询条件
    List<UniversalModel> Model = new ArrayList<>();
    private View viewkre;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewkre = View.inflate(this, R.layout.activity_red_packets, null);
        setContentView(viewkre);

        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        options = ImgUtils.mImageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

        View vHead = View.inflate(context, R.layout.redpackers_listviewaddheaderview, null);

        envelopes_money = (TextView) vHead.findViewById(R.id.envelopes_money);//余额
        personal_icon = (CircleImageView) vHead.findViewById(R.id.personal_icon);//头像
        red_envelopes = (TextView) vHead.findViewById(R.id.red_envelopes);//抢到红包
        balance = (TextView) vHead.findViewById(R.id.balance);//活动红包总额
        year = (TextView) vHead.findViewById(R.id.year);//年
        year_layout = (LinearLayout) vHead.findViewById(R.id.year_layout);//选择年份
        year.setText(str + "年");
        envelopes_money.setOnClickListener(this);
        year_layout.setOnClickListener(this);
        listview.addHeaderView(vHead);
        adapter = new RedPacketsActivityAdapter(context);
        listview.setAdapter(adapter);
        initData();
        processconditions();
    }

    private void initData() {
        MineFragmentModel mineFragmentModel = UserInfoUtils.readUserInfo(this);
        //头像
        imageLoader.displayImage(Constant.HTTP + mineFragmentModel.getAvatar(), personal_icon, options);
        envelopes_money.setText(mineFragmentModel.getRedenvelope());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.envelopes_money:
                Intent intent = new Intent(context, WithdrawalBalanceActivity.class);
                startActivity(intent);
                break;
            case R.id.year_layout:
                processyear();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        processpviewredpackets();
    }

    //生成查询条件
    private void processconditions() {
        Calendar c = Calendar.getInstance();//
        int year = c.get(Calendar.YEAR) - 2017 + 1;// 查询年份数量=获取当前年份-初始年份+1
        for (int i = 0; i < year; i++) {
            UniversalModel universalModel = new UniversalModel();
            universalModel.setYear((2017 + 0) + "");
            Model.add(universalModel);
        }
    }

    //绑定年份
    private void processyear() {
        UniversalPhotoPop typephoto = new UniversalPhotoPop(context, Model, 0);
        typephoto.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        typephoto.setAddresskListener(new UniversalPhotoPop.OnAddressCListener() {
            @Override
            public void onClick(String province, String city, String area) {
                str = province;
                year.setText(str + "年");
                processpviewredpackets();
            }
        });
    }

    //查看红包
    private void processpviewredpackets() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);

            HttpClient.viewredpackets(str, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (getviewredpackets != null) {
            getviewredpackets.clear();
        }
        System.out.println("stringmd5result" + result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote != null || getnote.size() > 0) {
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")) {

                String allmoney = getnote.get(0).getAllmoney();
                String grabARedEnvelope = getnote.get(0).getGrabARedEnvelope();
                String movableRedEnvelope = getnote.get(0).getMovableRedEnvelope();
                String content = getnote.get(0).getContent();
                envelopes_money.setText(allmoney);
                red_envelopes.setText(grabARedEnvelope);
                balance.setText(movableRedEnvelope);
                if (!TextUtils.isEmpty(content)) {
                    getviewredpackets = JsonTools.getviewredpackets(content);
                    if (getviewredpackets != null || getviewredpackets.size() > 0) {
                        adapter.addItem(getviewredpackets);
                    }
                }else {
                    ToastUtils.show(context, getMsg);
                }
            } else if (status.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            } else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }

        System.out.println("stringmd5result" + ex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }

        System.out.println("stringmd5result" + cex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}

